﻿using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRepository;
using BusinessRule;
using learun.util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nacos.AspNetCore;
using Newtonsoft.Json.Linq;
using ProjectManApi.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManApi.Controllers.xmgl
{
    public class ProLiXiangController : MvcControllerBase
    {
        private readonly INacosServerManager _serverManager;
        public ProLiXiangController(INacosServerManager serverManager) : base(serverManager)
        {
            _serverManager = serverManager;
        }
        #region 项目标段（ProLiXiangAdd.aspx 标段列表，获取单个标段，新增修改标段，删除标段）


        [HttpGet]
        public async Task<IActionResult> GetbidsectionIndexList([FromQuery] CProbidsectionQuerry querry)
        {
            try
            {
                List<string> list = new List<string>();
                (IEnumerable<dynamic>, int) SelectTable = await ProLiXiangRule.GetbidsectionIndexList(querry, await GetDepId());

                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(querry.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpGet]
        public async Task<IActionResult> GetXMLXIndexList([FromQuery] CProManageQuerry querry)
        {
            try
            {
                //CProManageQuerry querry = new CProManageQuerry();
                ////基础属性
                //querry.size = CommonHelper.ParseInt(size);
                //querry.current = CommonHelper.ParseInt(current);
                //querry.sort = sort;
                //querry.column = column;


                //querry.jsdw = jsdw;
                //querry.xmmc = xmmc;
                //querry.xmfl = xmflname;
                //querry.zjly = zjly;

                //querry.year = year;
                //querry.status = status;

                List<string> list = new List<string>();
                (IEnumerable<dynamic>, int) SelectTable = await ProLiXiangRule.GetXMlxIndexList(querry, await GetDepId());

                foreach (dynamic item in SelectTable.Item1)
                {
                    //这里是id
                    string id = item.id;
                    list.Add(id);
                }

                List<JavaApi.SPSDataItem> spitems = await JavaApi.GetSPStatusJavaAPI(querry.formCode, GetUserAccount(), list, await GetICApiInfo());
                foreach (dynamic item in SelectTable.Item1)
                {
                    string id = item.id;
                    var spstatus = spitems.Where(ii => ii.businessId == id).FirstOrDefault();

                    item.panguData = GetPanGudData(spstatus, item.spstatus);
                }

                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        [HttpGet]
        public async Task<IActionResult> GetXMDXIndexList(string xmmc, string dxmc, string jsdw, string xmlx, string year, string current = "1", string size = "20", string sort = "desc", string column = "id")
        {
            try
            {
                dynamic querry = new ExpandoObject();
                //基础属性
                querry.size = CommonHelper.ParseInt(size);
                querry.current = CommonHelper.ParseInt(current);
                querry.sort = sort;
                querry.column = column;


                querry.jsdw = jsdw;
                querry.xmmc = xmmc;
                querry.dxmc = dxmc;
                querry.xmlx = xmlx;

                querry.year = year;
                querry.haszb = "";

                (IEnumerable<dynamic>, int) SelectTable = await ProLiXiangRule.GetXMDXIndexList(querry, await GetDepId());


                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        //选择未采购弹窗
        [HttpGet]
        public async Task<IActionResult> GetXMDXIndexListC(string xmmc, string dxmc, string jsdw, string xmlx, string year, string current = "1", string size = "20", string sort = "desc", string column = "id")
        {
            try
            {
                dynamic querry = new ExpandoObject();
                //基础属性
                querry.size = CommonHelper.ParseInt(size);
                querry.current = CommonHelper.ParseInt(current);
                querry.sort = sort;
                querry.column = column;


                querry.jsdw = jsdw;
                querry.xmmc = xmmc;
                querry.dxmc = dxmc;
                querry.xmlx = xmlx;

                querry.year = year;
                querry.hascg = "1";

                (IEnumerable<dynamic>, int) SelectTable = await ProLiXiangRule.GetXMDXIndexList(querry, await GetDepId());


                Page pa = new Page();
                pa.records = SelectTable.Item1;
                pa.total = SelectTable.Item2;
                return Success(pa);
            }
            catch (Exception ex)
            {
                return Fail("响应失败" + ex);
            }
        }

        /// <summary>
        /// 标段列表-固定单体pid=0,标段pid=固定单体id
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="xmid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetBDListByXmid(string pid, string xmid)
        {
            try
            {
                var SelectTable = await ProLiXiangRule.GetBDListByXmid(pid, xmid);

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 获取标段树结构一级节点列表 标段右边树格式
        /// </summary>
        /// <param name="xmid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetBDLXByXmid(string xmid)
        {
            try
            {
                var SelectTable = await ProLiXiangRule.GetBDLXByXmid(xmid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 获取标段树结构列表
        /// </summary>
        /// <param name="xmid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetBDTreeByXmid(string xmid)
        {
            try
            {
                var SelectTable = await ProLiXiangRule.GetBDTreeByXmid(xmid);
                return Success(SelectTable);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        /// <summary>
        /// 根据id获取单条标段
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetBDById(string keyValue)
        {
            try
            {
                var SelectTable = await ProLiXiangRule.GetBDById(keyValue);

                if (null == SelectTable)
                {
                    SelectTable = new ExpandoObject();
                }
                try
                {
                    //单项id
                    SelectTable.zbdw = await ProZhaoBiaoRule.GetZBDWXXByBDId(keyValue);

                    //立项合并
                    var LXHB = await ProLiXiangRule.GetlxHBbydxid(keyValue);
                    var gsids = LXHB.Select(l => l.hbid).ToList<string>();
                    SelectTable.gsids = gsids;
                }
                catch
                {
                }

                return Success(SelectTable);
            }
            catch (Exception)
            {
                return Fail("响应失败");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cprolxs"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        //CProManageLX prolx, List<string> gsids, string ope
        public async Task<IActionResult> InsertCProManageLX(dynamic cprolxs)
        {
            //            {
            //                "ope":"py",
            //"gsids":[
            //"1",
            //"2"
            //],
            //"prolx":{
            //                    "id": "string",
            //  "xmid": "string",
            //  "pid": "string",
            //  "dxmc": "string",
            //  "lxspzx": "string",
            //  "lxsprq": "2021-05-11T09:49:01.668Z",
            //  "spl": "string",
            //  "spr": "string",
            //  "remark": "string",
            //  "status": "string",
            //  "createdate": "2021-05-11T09:49:01.668Z",
            //  "createuser": "string",
            //  "gspsje": 0,
            //  "lxje": 0
            //}
            //            }

            string strope = "";
            CProManageLX prolx = new CProManageLX();
            List<string> gsids = new List<string>();
            try
            {
                JObject job = (JObject)cprolxs;

                //获取方法
                strope = cprolxs.ope;

                //获取实体类 这样复杂类型不用定义实体类
                prolx = job["prolx"].ToObject<CProManageLX>();

                if (null != job["gsids"])
                    gsids = job["gsids"].ToObject<string[]>().ToList();

                if (strope != "cf" && strope != "py" && strope != "hb")
                {
                    //只能是[py,cf,bh]
                    return Fail("ope参数类型错误");
                }

                //自己校验合并还是平移
                if (strope == "py")
                {
                    if (null != gsids && gsids.Count > 1)
                    {
                        strope = "hb";
                    }
                }
            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {

                if (string.IsNullOrEmpty(prolx.xmid))
                {
                    return Fail("项目编号不能为空");
                }
                if (string.IsNullOrEmpty(prolx.dxmc))
                {
                    return Fail("单项名称不能为空");
                }

                prolx.dxbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.单项.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(cprolxs), await GetICApiInfo());

                string Cuid = GetUserId();
                if (strope == "py")
                {

                    string strlx = await CProTZJHRule.GetFunGLListBylxEunm(BaseEditPower.JumpModule.概算.ToString());
                    if (strlx == "1")
                    {
                        if (gsids.Count != 1)
                        {
                            return Fail("概算id参数异常");
                        }

                        prolx.id = CommonHelper.GetUniqueId().ToString();


                        prolx.createuser = Cuid;
                        prolx.status = BaseEditPower.GetSPEnumValue("通过");

                        CProManageLXHB lxhb = new CProManageLXHB();
                        lxhb.id = CommonHelper.GetUniqueId().ToString();
                        lxhb.dxid = prolx.id;
                        lxhb.hbid = gsids[0];

                        CProManageGS gs = new CProManageGS();
                        gs.id = gsids[0];
                        gs.zt = "py";

                        await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, gs, lxhb, strope,"add", GetUserId());
                    }
                    else
                    {
                        prolx.id = CommonHelper.GetUniqueId().ToString();
                        prolx.createuser = Cuid;

                        if (string.IsNullOrEmpty(prolx.xmid))
                        {
                            return Fail("标段信息xmid不能为空");
                        }
                        if (string.IsNullOrEmpty(prolx.pid))
                        {
                            return Fail("标段信息pid不能为空");
                        }

                        prolx.id = CommonHelper.GetUniqueId().ToString();
                        prolx.createuser = Cuid;
                        prolx.status = BaseEditPower.GetSPEnumValue("通过");

                        //新增标段
                        await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, "add");
                    }
                }
                else if (strope == "cf")
                {
                    if (gsids.Count != 1)
                    {
                        return Fail("概算id参数异常");
                    }

                    prolx.id = CommonHelper.GetUniqueId().ToString();
                    prolx.createuser = Cuid;
                    prolx.status = BaseEditPower.GetSPEnumValue("通过");

                    CProManageLXHB lxhb = new CProManageLXHB();
                    lxhb.id = CommonHelper.GetUniqueId().ToString();
                    lxhb.dxid = prolx.id;
                    lxhb.hbid = gsids[0];

                    CProManageGS gs = new CProManageGS();
                    gs.id = gsids[0];
                    gs.zt = "cf";

                    await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, gs, lxhb, strope, "add", GetUserId());
                }
                else if (strope == "hb")
                {
                    if (gsids.Count < 2)
                    {
                        return Fail("概算id参数异常");
                    }

                    prolx.id = CommonHelper.GetUniqueId().ToString();
                    prolx.createuser = Cuid;
                    prolx.status = BaseEditPower.GetSPEnumValue("通过");

                    List<CProManageLXHB> listhb = new List<CProManageLXHB>();
                    List<CProManageGS> listgs = new List<CProManageGS>();
                    for (int i = 0; i < gsids.Count; i++)
                    {
                        CProManageLXHB lxhb = new CProManageLXHB();
                        lxhb.id = CommonHelper.GetUniqueId().ToString();
                        lxhb.dxid = prolx.id;
                        lxhb.hbid = gsids[i];
                        listhb.Add(lxhb);

                        CProManageGS gs = new CProManageGS();
                        gs.id = gsids[i];
                        gs.zt = "hb";

                        listgs.Add(gs);
                    }


                    await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, listgs, listhb, strope, "add", GetUserId());
                }

                return Success("成功");
            }
            catch (Exception ex)
            {
                //return Fail("响应失败");
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cprolxs"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        //CProManageLX prolx, List<string> gsids, string ope
        public async Task<IActionResult> InsertCProManageLXSubmit(dynamic cprolxs)
        {
            //            {
            //                "ope":"py",
            //"gsids":[
            //"1",
            //"2"
            //],
            //"prolx":{
            //                    "id": "string",
            //  "xmid": "string",
            //  "pid": "string",
            //  "dxmc": "string",
            //  "lxspzx": "string",
            //  "lxsprq": "2021-05-11T09:49:01.668Z",
            //  "spl": "string",
            //  "spr": "string",
            //  "remark": "string",
            //  "status": "string",
            //  "createdate": "2021-05-11T09:49:01.668Z",
            //  "createuser": "string",
            //  "gspsje": 0,
            //  "lxje": 0
            //}
            //            }

            string strope = "";
            string straction = "";
            CProManageLX prolx = new CProManageLX();
            List<string> gsids = new List<string>();
            FileUploadent filecodes = new FileUploadent();
            try
            {
                JObject job = (JObject)cprolxs;

                //获取方法
                straction = cprolxs.action;
                strope = cprolxs.ope;

                //获取实体类 这样复杂类型不用定义实体类
                prolx = job["prolx"].ToObject<CProManageLX>();

                //接收附件列表
                if (null != job["apiargs"])
                    filecodes = job["apiargs"].ToObject<FileUploadent>();

                if (null != job["gsids"])
                    gsids = job["gsids"].ToObject<string[]>().ToList();

                if (straction != "cf" && straction != "py" && straction != "hb")
                {
                    //只能是[py,cf,bh]
                    return Fail("action参数类型错误");
                }

                if (strope != "add" && strope != "submit" && strope != "edit")//&& strope != "edit" 
                {
                    return Fail("ope参数不合法");
                }

                //大于1 
                if (null != gsids && gsids.Count > 1)
                {
                    straction = "hb";
                }

            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {

                string strlsope = strope;
                if (strope == "submit")
                {
                    if (string.IsNullOrEmpty(prolx.id))//完成新增
                    {
                        strope = "add";
                    }
                    else
                    {
                        CProManageLX jhe = await TranstionRule.FindEntityByKey<CProManageLX>(prolx.id);
                        if (null == jhe)
                        {
                            strope = "add";
                        }
                        else
                        {
                            strope = "edit";
                        }
                    }
                    prolx.status = BaseEditPower.GetSPEnumValue("待审核");
                }



                if (string.IsNullOrEmpty(prolx.xmid))
                {
                    return Fail("项目编号不能为空");
                }
                if (string.IsNullOrEmpty(prolx.dxmc))
                {
                    return Fail("单项名称不能为空");
                }

                prolx.dxbm = await JavaApi.BusinessBMJavaAPI(BaseEditPower.listTableName[BaseEditPower.TableName.单项.ToString()], Newtonsoft.Json.JsonConvert.SerializeObject(cprolxs), await GetICApiInfo());

                string Cuid = GetUserId();
                if (straction == "py")
                {

                    string strlx = await CProTZJHRule.GetFunGLListBylxEunm(BaseEditPower.JumpModule.概算.ToString());
                    if (strlx == "1")
                    {
                        if (gsids.Count != 1)
                        {
                            return Fail("概算id参数异常");
                        }

                        if (strope == "add")
                            prolx.id = CommonHelper.GetUniqueId().ToString();


                        prolx.createuser = Cuid;
                        prolx.createdate = DateTime.Now;
                        //prolx.status = BaseEditPower.GetSPEnumValue("通过");

                        if (strlsope != "submit")
                            prolx.status = BaseEditPower.GetSPEnumValue("待提交");

                        if (strlsope == "submit")
                        {
                            var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.单项.ToString(), prolx.xmid, prolx.id);

                            JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = prolx.dxbm, amount = prolx.lxje?.ToString(), content = "[标段]" + prolx.dxmc, deptName = GetUserDeptName() };

                            JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.单项.ToString(), prolx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cprolxs), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                            //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                            string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                            prolx.spl = strInstanceId;
                            if (strInstanceId.StartsWith("err"))
                            {
                                return Fail("提交失败，启动工作流异常");
                            }
                        }

                        CProManageLXHB lxhb = new CProManageLXHB();
                        lxhb.id = CommonHelper.GetUniqueId().ToString();
                        lxhb.dxid = prolx.id;
                        lxhb.hbid = gsids[0];

                        CProManageGS gs = new CProManageGS();
                        gs.id = gsids[0];
                        gs.zt = "py";

                        await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, gs, lxhb, straction, strope, GetUserId());
                    }
                    else
                    {
                        if (strope == "add")
                            prolx.id = CommonHelper.GetUniqueId().ToString();
                        prolx.createuser = Cuid;

                        if (string.IsNullOrEmpty(prolx.xmid))
                        {
                            return Fail("标段信息xmid不能为空");
                        }
                        if (string.IsNullOrEmpty(prolx.pid))
                        {
                            return Fail("标段信息pid不能为空");
                        }

                        //prolx.id = CommonHelper.GetUniqueId().ToString();
                        //prolx.createuser = Cuid;
                        prolx.createdate = DateTime.Now;

                        //prolx.status = BaseEditPower.GetSPEnumValue("通过");
                        if (strlsope != "submit")
                            prolx.status = BaseEditPower.GetSPEnumValue("待提交");

                        if (strlsope == "submit")
                        {
                            var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.单项.ToString(), prolx.xmid, prolx.id);

                            JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = prolx.dxbm, amount = prolx.lxje?.ToString(), content = "[标段]" + prolx.dxmc, deptName = GetUserDeptName() };

                            JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.单项.ToString(), prolx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cprolxs), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                            //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                            string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                            prolx.spl = strInstanceId;
                            if (strInstanceId.StartsWith("err"))
                            {
                                return Fail("提交失败，启动工作流异常");
                            }
                        }

                        //新增标段
                        await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, strope);
                    }
                }
                else if (straction == "cf")
                {
                    if (gsids.Count != 1)
                    {
                        return Fail("概算id参数异常");
                    }

                    if (strope == "add")
                        prolx.id = CommonHelper.GetUniqueId().ToString();

                    prolx.createuser = Cuid;
                    prolx.createdate = DateTime.Now;

                    //prolx.status = BaseEditPower.GetSPEnumValue("通过");
                    if (strlsope != "submit")
                        prolx.status = BaseEditPower.GetSPEnumValue("待提交");

                    if (strlsope == "submit")
                    {
                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.单项.ToString(), prolx.xmid, prolx.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = prolx.dxbm, amount = prolx.lxje?.ToString(), content = "[标段]" + prolx.dxmc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.单项.ToString(), prolx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cprolxs), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        prolx.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    CProManageLXHB lxhb = new CProManageLXHB();
                    lxhb.id = CommonHelper.GetUniqueId().ToString();
                    lxhb.dxid = prolx.id;
                    lxhb.hbid = gsids[0];

                    CProManageGS gs = new CProManageGS();
                    gs.id = gsids[0];
                    gs.zt = "cf";

                    await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, gs, lxhb, straction, strope, GetUserId());
                }
                else if (straction == "hb")
                {
                    if (gsids.Count < 2)
                    {
                        return Fail("概算id参数异常");
                    }

                    if (strope == "add")
                        prolx.id = CommonHelper.GetUniqueId().ToString();
                    prolx.createuser = Cuid;
                    prolx.createdate = DateTime.Now;

                    //prolx.status = BaseEditPower.GetSPEnumValue("通过");
                    if (strlsope != "submit")
                        prolx.status = BaseEditPower.GetSPEnumValue("待提交");

                    if (strlsope == "submit")
                    {
                        var url = BaseEditPower.GetSPDetailUrlS(BaseEditPower.TableName.单项.ToString(), prolx.xmid, prolx.id);

                        JavaApi.SPLEnt.Auditdescription _description = new JavaApi.SPLEnt.Auditdescription() { businessNo = prolx.dxbm, amount = prolx.lxje?.ToString(), content = "[标段]" + prolx.dxmc, deptName = GetUserDeptName() };

                        JavaApi.SPLEnt spl = new JavaApi.SPLEnt(BaseEditPower.TableName.单项.ToString(), prolx.id, filecodes.formCode, Newtonsoft.Json.JsonConvert.SerializeObject(cprolxs), "http://ic-xmzc/netapi/ProTZJH/UpdateStatus", url.Item1, url.Item2, _description);

                        //bsp = await JavaApi.startInstanceJavaAPI(spl, await GetICApiInfo());
                        string strInstanceId = await JavaApi.startInstanceJavaAPIStr(spl, await GetICApiInfo());
                        prolx.spl = strInstanceId;
                        if (strInstanceId.StartsWith("err"))
                        {
                            return Fail("提交失败，启动工作流异常");
                        }
                    }

                    List<CProManageLXHB> listhb = new List<CProManageLXHB>();
                    List<CProManageGS> listgs = new List<CProManageGS>();
                    for (int i = 0; i < gsids.Count; i++)
                    {
                        CProManageLXHB lxhb = new CProManageLXHB();
                        lxhb.id = CommonHelper.GetUniqueId().ToString();
                        lxhb.dxid = prolx.id;
                        lxhb.hbid = gsids[i];
                        listhb.Add(lxhb);

                        CProManageGS gs = new CProManageGS();
                        gs.id = gsids[i];
                        gs.zt = "hb";

                        listgs.Add(gs);
                    }


                    await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, listgs, listhb, straction, strope, GetUserId());
                }

                return Success("成功");
            }
            catch (Exception ex)
            {
                //return Fail("响应失败");
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cprolxs"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        //CProManageLX prolx, List<string> gsids, string ope
        public async Task<IActionResult> InsertCProManageLXSig(dynamic cprolxs)
        {
            string strope = "";
            CProManageLX prolx = new CProManageLX();
            //List<string> gsids = new List<string>();
            try
            {
                JObject job = (JObject)cprolxs;

                //获取方法
                strope = cprolxs.ope;

                //获取实体类 这样复杂类型不用定义实体类
                prolx = job["prolx"].ToObject<CProManageLX>();


                if (strope != "add" && strope != "edit")
                {
                    //只能是[py,cf,bh]
                    return Fail("ope参数类型错误");
                }
            }
            catch (Exception ex)
            {
                return Fail("参数转换出错：" + ex.Message);
            }

            try
            {
                //if (string.IsNullOrEmpty(prolx.xmid))
                //{
                //    return Fail("标段信息xmid不能为空");
                //}
                //if (string.IsNullOrEmpty(prolx.pid))
                //{
                //    return Fail("标段信息pid不能为空");
                //}
                //if (string.IsNullOrEmpty(prolx.dxmc))
                //{
                //    return Fail("标段信息dxmc不能为空");
                //}

                string Cuid = GetUserId();
                if (strope == "add")
                {
                    string strlx = await CProTZJHRule.GetFunGLListBylxEunm(BaseEditPower.JumpModule.概算.ToString());
                    if (strlx == "1")
                    {
                        return Fail("不能直接新增标段信息");
                    }

                    prolx.id = CommonHelper.GetUniqueId().ToString();
                    prolx.createuser = Cuid;

                    if (string.IsNullOrEmpty(prolx.xmid))
                    {
                        return Fail("标段信息xmid不能为空");
                    }
                    if (string.IsNullOrEmpty(prolx.pid))
                    {
                        return Fail("标段信息pid不能为空");
                    }

                    prolx.id = CommonHelper.GetUniqueId().ToString();
                    prolx.createuser = Cuid;
                    prolx.status = BaseEditPower.GetSPEnumValue("通过");

                    //新增标段
                    await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, strope);
                }
                else if (strope == "edit")
                {
                    if (string.IsNullOrEmpty(prolx.id))
                    {
                        return Fail("标段信息id不能为空");
                    }

                    //if (string.IsNullOrEmpty(prolx.pid))
                    //{
                    //    return Fail("标段信息pid不能为空");
                    //}

                    //prolx.id = CommonHelper.GetUniqueId().ToString();
                    prolx.createuser = Cuid;

                    //修改标段
                    await BusinessRule.ProLiXiangRule.InsertCProManageLX(prolx, strope);
                }
               
                return Success("成功");
            }
            catch (Exception ex)
            {
                //return Fail("响应失败");
                return Fail(ex.Message);
            }
        }


        /// <summary>
        /// 修改立项信息
        /// </summary>
        /// <param name="prolx"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]

        public async Task<IActionResult> UpdateCProManageLX(CProManageLX prolx)
        {
            try
            {
                if (string.IsNullOrEmpty(prolx.id))
                {
                    return Fail("单项编号不能为空");
                }
                if (string.IsNullOrEmpty(prolx.xmid))
                {
                    return Fail("项目编号不能为空");
                }
                if (string.IsNullOrEmpty(prolx.dxmc))
                {
                    return Fail("单项名称不能为空");
                }

                await BusinessRule.ProLiXiangRule.UpdateCProManageLX(prolx, GetUserId());

                return Success("成功");
            }
            catch (Exception ex)
            {
                //return Fail("响应失败");
                return Fail(ex.Message);
            }
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> DeleteCProManageLXMulti(Root ids)
        {
            try
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    CProManageLX count = await new RepositoryFactory().BaseRepository().FindEntityByKey<CProManageLX>(idm.id);
                    if (count is null)
                    {
                        throw new Exception("没有数据");
                    }
                    if (count.pid == "0")
                    {
                        throw new Exception("不可删除父节点");
                    }

                    string sql = "select ISNULL(sqje,0.00)sqje from View_ProFKSQLists where dxid = '" + idm.id + "'";
                    DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
                    string str2 = "0";
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        str2 = table.Rows[i]["sqje"].ToString();
                    }
                    if ((str2 != "0.00") && (str2 != "0"))
                    {
                        throw new Exception("该标段已有付款，不能删除");
                    }
                }

                await BusinessRule.ProLiXiangRule.DeleteCProManageLXMulti(ids);

                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        #endregion


    }
}
